﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPFLocalizeExtension.Engine;
using WPFLocalizeExtension.Extensions;
using System.Windows;
using System.Windows.Threading;

namespace LockCrypt {
    public class I18nUtils {
        /// <summary>
        /// Gets a localized version of a string.
        /// </summary>
        /// <param name="bundle">The name of the resource bundle.</param>
        /// <param name="key">The translation key.</param>
        /// <returns>A localized version of the string representyed by <c>key</c></returns>
        public static string GetString(string bundle, string key) {
            string translated = string.Empty;
            Application.Current.Dispatcher.Invoke((DispatcherPriority.Normal), new Action(() => {
                LocTextExtension res = new LocTextExtension() { Assembly = "LockCrypt.Core", Dict = bundle, Key = key };
                res.ResolveLocalizedValue(out translated);
            }));
            return translated;
        }

        /// <summary>
        /// Gets a localized version of a formatted string.
        /// </summary>
        /// <param name="bundle">The name of the resource bundle.</param>
        /// <param name="key">The translation key.</param>
        /// <param name="formatArguments">The format arguments.</param>
        /// <returns>
        /// A localized version of the string representyed by <c>key</c>
        /// </returns>
        public static string GetString(string bundle, string key, params string[]  formatArguments ) {
            string translated = string.Empty;
            Application.Current.Dispatcher.Invoke((DispatcherPriority.Normal), new Action(() => {
                LocTextExtension res = new LocTextExtension() { Assembly = "LockCrypt.Core", Dict = bundle, Key = key };
                int numSpecifiers = formatArguments.Count();
                if(numSpecifiers > 0) { // blame the i18n extension for crap implementation
                    res.FormatSegment1 = formatArguments[0];
                    if(numSpecifiers > 1) {
                        res.FormatSegment2 = formatArguments[1];
                        if(numSpecifiers > 2) {
                            res.FormatSegment3 = formatArguments[2];
                            if(numSpecifiers > 3) {
                                res.FormatSegment4 = formatArguments[3];
                                if(numSpecifiers > 4) {
                                    res.FormatSegment5 = formatArguments[4];
                                }
                            }
                        }
                    }
                }
                res.ResolveLocalizedValue(out translated);
            }));
            return translated;
        }
    }
}
